﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.WorkflowEditor
{

	/// <summary>
	/// Declaration of CachedData class
	/// Singleton pattern class for storing supporting data
	/// </summary>
	public class CachedData : CachedDataBase
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		public static CachedData Singleton
		{
			get { return m_SingletonBase as CachedData; }
		}

		public ObservableCollection<string> PhoneCallMessages
		{
			get { return _safeFindStringListByCategory(Definitions.ReusableString_PhoneCallMessages); }
		}

		public ObservableCollection<string> PhoneCallReasons
		{
			get { return _safeFindStringListByCategory(Definitions.ReusableString_PhoneCallReasons); }
		}

		private List<CommBodySummaryTransport> m_commBodyList = null;
		public List<CommBodySummaryTransport> CommBodies
		{
			get { return m_commBodyList; }
		}

		private Dictionary<int, string> m_dictPopups = null;
		public Dictionary<int, string> PopupDictionary
		{
			get { return m_dictPopups; }
		}

		private Dictionary<int,string> m_dictLetterTypes = null;
		public Dictionary<int, string> LetterTypeDictionary
		{
			get { return m_dictLetterTypes; }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		static CachedData()
		{

			// Create the single instance
			new CachedData();

		}

		/////////////////////////////////////////////////////////////
		// Operations - Adding data loaded from server

		public void AddIncoming(WorkflowSupportData supportData)
		{
#if DEBUG
			Debug.Assert(m_commBodyList == null);
#endif // #if DEBUG

			// Create a list of comm-bodies
			m_commBodyList = new List<CommBodySummaryTransport>();
			m_commBodyList.AddRange(supportData.CommBodyList);

			// Create dictionary of popups
			m_dictPopups = new Dictionary<int,string>();
			foreach (var di in supportData.PopupDictionary)
				m_dictPopups.Add(di.Key,di.Value);

			// Create lists of reusable strings
			base.AddReusableStrings( Definitions.ReusableString_PhoneCallReasons, supportData.PhoneCallReasons );
			base.AddReusableStrings( Definitions.ReusableString_PhoneCallMessages, supportData.PhoneCallMessages );

			// Create dictionary of letter-types
			m_dictLetterTypes = new Dictionary<int, string>();
			foreach (var di in supportData.LetterTypeDictionary)
				m_dictLetterTypes.Add(di.Key, di.Value);

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public string GetCommBodyDescription( CommBodyWrapper commBodyWrapper )
		{
#if DEBUG
			Debug.Assert(!commBodyWrapper.IsEmpty);
#endif // #if DEBUG

			// Declare return variable
			string strFtmp = null;

			// Find the comm-body
			CommBodySummaryTransport commBody = null;
			for (int i1 = 0; i1 < m_commBodyList.Count; i1++)
			{
				if (m_commBodyList[i1].CommBodyId == commBodyWrapper.CommBodyId)
				{
					commBody = m_commBodyList[i1];
					break;
				}
			}
			if (commBody != null)
			{
				if(commBody.SectionList.Count == 0)
					strFtmp = commBody.Name;
				else
				{
					StringBuilder sb = new StringBuilder();
					sb.Append(commBody.Name);
					sb.Append(" [");
					for (int i1 = 0; i1 < commBodyWrapper.Sections.Count; i1++)
					{
						if (i1 > 0)
							sb.Append(",");
						sb.Append(commBodyWrapper.Sections[i1]);
					}
					sb.Append("]");
					strFtmp = sb.ToString();
				}
			}
#if DEBUG
			else
				Debug.Assert(false);
#endif // #if DEBUG

			// Return variable
			return strFtmp;

		}

		public string GetPopupName(int iPopupId)
		{

			// Declare return variable
			string strFtmp = null;

			// Find popup name
			if (m_dictPopups.ContainsKey(iPopupId))
				strFtmp = m_dictPopups[iPopupId];

			// Return variable
			return strFtmp;

		}

		public string GetLetterTypeName(int iLetterTypeId)
		{

			// Declare return variable
			string strFtmp = null;

			// Find popup name
			if (m_dictLetterTypes.ContainsKey(iLetterTypeId))
				strFtmp = m_dictLetterTypes[iLetterTypeId];

			// Return variable
			return strFtmp;

		}

		/////////////////////////////////////////////////////////////
		// Static Operations

#if USEFAKEDATA
		public static FRL.DebtChased.Silverlight.Library.RichContentService.WorkflowSupportData GetDummySupportingData()
		{

			// Declare return variable
			FRL.DebtChased.Silverlight.Library.RichContentService.WorkflowSupportData supportData = new WorkflowSupportData();

			// Load the comm-bodies
			supportData.CommBodyList = new ObservableCollection<CommBodySummaryTransport>();

			CommBodySummaryTransport newBody = new CommBodySummaryTransport() { CommBodyId = 1, CommBodyType = (int)Definitions.eCommBodyType.Letter, Name = "First Letter" };
			newBody.SectionList = new ObservableCollection<string>();
			newBody.SectionList.Add("COMMERCIAL");
			newBody.SectionList.Add("CONSUMER");
			supportData.CommBodyList.Add(newBody);

			newBody = new CommBodySummaryTransport() { CommBodyId = 2, CommBodyType = (int)Definitions.eCommBodyType.Email, Name = "Letter Cover" };
			newBody.SectionList = new ObservableCollection<string>();
			supportData.CommBodyList.Add(newBody);

			newBody = new CommBodySummaryTransport() { CommBodyId = 3, CommBodyType = (int)Definitions.eCommBodyType.SMS, Name = "Payment Reminder" };
			newBody.SectionList = new ObservableCollection<string>();
			supportData.CommBodyList.Add(newBody);

			newBody = new CommBodySummaryTransport() { CommBodyId = 4, CommBodyType = (int)Definitions.eCommBodyType.Letter, Name = "Legal Action Pending" };
			newBody.SectionList = new ObservableCollection<string>();
			newBody.SectionList.Add("TYPEA");
			newBody.SectionList.Add("TYPEB");
			supportData.CommBodyList.Add(newBody);

			// Load the popup dictionary
			supportData.PopupDictionary = new Dictionary<int, string>();
			supportData.PopupDictionary.Add(1, "Qualify Trading Style");
			supportData.PopupDictionary.Add(2, "Legal Action Consequence");

			// Load the call-debtor reasons
			supportData.PhoneCallReasons = new System.Collections.ObjectModel.ObservableCollection<string>();
			supportData.PhoneCallReasons.Add("Chase Debt");
			supportData.PhoneCallReasons.Add("Request Information");
			supportData.PhoneCallReasons.Add("Payment Reminder");

			// Load the call-debtor messages
			supportData.PhoneCallMessages = new System.Collections.ObjectModel.ObservableCollection<string>();
			supportData.PhoneCallMessages.Add("See notes for details");
			supportData.PhoneCallMessages.Add("Ask for mobile phone number");

			// Load the letter types
			supportData.LetterTypeDictionary = new Dictionary<int, string>();
			supportData.LetterTypeDictionary.Add(0, Definitions.None);
			supportData.LetterTypeDictionary.Add(1, "First Chase Letter");
			supportData.LetterTypeDictionary.Add(2, "First LBA");

			// return object
			return supportData;

		}
#endif // #if USEFAKEDATA

#if USEFAKEDATA
		public static WorkflowsTransportWrapper GetDummyWorkflowList(int iClientId)
		{

			// Declare return variable
			WorkflowsTransportWrapper Ftmp = new WorkflowsTransportWrapper();

			// Add invoice status list
			Ftmp.AccountInvoiceStatusList = new ObservableCollection<AccountInvoiceStatusTransport>();
			Ftmp.AccountInvoiceStatusList.Add(new AccountInvoiceStatusTransport() { AccountInvoiceStatusId = 1, InvoiceStatusName = "NI", ClientWorkflowId = 8, DefaultWorkflowId = 8, HasGlobalScope = true, InUse = true, IsInstalmentPlanStatus = false });
			Ftmp.AccountInvoiceStatusList.Add(new AccountInvoiceStatusTransport() { AccountInvoiceStatusId = 2, InvoiceStatusName = "Paid", ClientWorkflowId = 9, DefaultWorkflowId = 9, HasGlobalScope = true, InUse = true, IsInstalmentPlanStatus = false });
			Ftmp.AccountInvoiceStatusList.Add(new AccountInvoiceStatusTransport() { AccountInvoiceStatusId = 3, InvoiceStatusName = "Part Paid", ClientWorkflowId = 23, DefaultWorkflowId = 23, HasGlobalScope = true, InUse = true, IsInstalmentPlanStatus = false });
			Ftmp.AccountInvoiceStatusList.Add(new AccountInvoiceStatusTransport() { AccountInvoiceStatusId = 4, InvoiceStatusName = "Instalment 1", ClientWorkflowId = 101, DefaultWorkflowId = 101, HasGlobalScope = true, InUse = true, IsInstalmentPlanStatus=true });
			if (iClientId == 0)
			{

			}
			else if (iClientId == 13991)
			{
				Ftmp.AccountInvoiceStatusList[2].ClientWorkflowId = 13;
			}
			else if (iClientId == 13992)
			{
				Ftmp.AccountInvoiceStatusList[2].ClientWorkflowId = 22;
			}
			else
				Debug.Assert(false);
			
			// Add workflows
			Ftmp.WorkflowList = new ObservableCollection<WorkflowSummaryTransport>();
			Ftmp.WorkflowList.Add(new WorkflowSummaryTransport() { WorkflowId = 8, WorkflowName = "NI (GB)", Summary = "summary", IsSuperceeded = false, UsageCount = 0, InvoiceStatusId = 1 });
			Ftmp.WorkflowList.Add(new WorkflowSummaryTransport() { WorkflowId = 9, WorkflowName = "Dev A", Summary = "summary", IsSuperceeded = true, UsageCount = 1, InvoiceStatusId = 2 });
			Ftmp.WorkflowList.Add(new WorkflowSummaryTransport() { WorkflowId = 13, WorkflowName = "Test Send Letter", Summary = "summary", IsSuperceeded = true, UsageCount = 0, InvoiceStatusId = 3 });
			Ftmp.WorkflowList.Add(new WorkflowSummaryTransport() { WorkflowId = 22, WorkflowName = "New Workflow", Summary = "summary", IsSuperceeded = false, UsageCount = 3, InvoiceStatusId = 3 });
			Ftmp.WorkflowList.Add(new WorkflowSummaryTransport() { WorkflowId = 23, WorkflowName = "Test Send Email", Summary = "summary", IsSuperceeded = false, UsageCount = 3, InvoiceStatusId = 3 });
			Ftmp.WorkflowList.Add(new WorkflowSummaryTransport() { WorkflowId = 101, WorkflowName = "Instalment 1", Summary = "summary", IsSuperceeded = false, UsageCount = 0, InvoiceStatusId = 4 });

			// Return variable
			return Ftmp;

		}
#endif // #if USEFAKEDATA

	}

}